package context.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

public class Parser
{
    
    
    
    
    public Parser()
    {
        
    }
    
    
    public final String parse(String aFullPath)
    {
        BufferedReader fin = this.genReader(aFullPath);
        LinkedList<String> lines = this.readFile(fin, aFullPath);
        String combinedSrc = Data.get().concatenate(lines);
        return combinedSrc;
    }
    
    
    private BufferedReader genReader(String aFullPath)
    {
        BufferedReader fin = null;
        try
        {
            fin = new BufferedReader(new FileReader(aFullPath));
        }
        catch (FileNotFoundException e)
        {
            System.err.println("FileNotFound: " + aFullPath);
        }
        return fin;
    }
    
    private LinkedList<String> readFile(
            BufferedReader aFileIn, 
            String aFullPath)
    {
        LinkedList<String> lines = new LinkedList<>();
        try
        {
            while (aFileIn.ready())
            {
                String line = aFileIn.readLine();
                if (!line.equals("") && !line.startsWith("//"))
                {
                    lines.add(line);
                }
            }
        }
        catch (IOException e)
        {
            System.err.println("Error reading file: " + aFullPath);
        }
        return lines;
    }
    
}
